package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.MenuDto;
import com.aoxin.model.dto.TreeDto;
import com.aoxin.model.opm.SlfMenu;
import com.aoxin.service.inter.opm.MenuService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

/**
 * 菜单Controller
 * 
 * @author Melody
 * @date 2018年3月21日 上午11:09:33
 */
@Controller
@ResponseBody
@RequestMapping("/web/menu")
public class MenuController {

	@Autowired
	private MenuService menuService;

	/**
	 * 添加菜单
	 * 
	 * @param menu
	 *            菜单
	 */
	@RequestMapping("/insertMenu")
	@LogInfo(content = "菜单", operateType = OperateType.INSERT)
	public AjaxRes insertMenu(SlfMenu menu, AjaxRes ajaxRes) {
		try {
			this.menuService.insertSlfMenu(menu);
			ajaxRes.setSucceedMsg(Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 删除菜单
	 * 
	 * @param menuId
	 *            菜单ID
	 */
	@RequestMapping("/deleteMenu")
	@LogInfo(content = "菜单", operateType = OperateType.DELETE)
	public AjaxRes deleteMenu(@RequestParam("menuId") Integer menuId, AjaxRes ajaxRes) {
		try {
			this.menuService.deleteSlfMenu(menuId);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除
	 * 
	 * @param menuIds
	 *            菜单ID集合
	 */
	@RequestMapping("/deleteMenus")
	@LogInfo(content = "菜单", operateType = OperateType.DELETE)
	public AjaxRes deleteMenus(@RequestParam("menuIds") List<Integer> menuIds, AjaxRes ajaxRes) {
		try {
			this.menuService.deleteSlfMenuByIds(menuIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 根据菜单ID查询菜单
	 * 
	 * @param menuId
	 *            菜单ID
	 * @return 菜单
	 */
	@RequestMapping("/getMenu")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes getMenu(@RequestParam("menuId") Integer menuId, AjaxRes ajaxRes) {
		try {
			SlfMenu slfMenu = this.menuService.getSlfMenu(menuId);
			ajaxRes.setSucceed(slfMenu, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改菜单
	 * 
	 * @param menu
	 *            菜单ID集合
	 */
	@RequestMapping("/updateMenu")
	@LogInfo(content = "菜单", operateType = OperateType.UPDATE)
	public AjaxRes updateMenu(SlfMenu menu, AjaxRes ajaxRes) {
		try {
			this.menuService.updateSlfMenu(menu);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改菜单状态
	 * 
	 * @param menuIds
	 *            菜单ID集合
	 */
	@RequestMapping("/updateMenuStates")
	@LogInfo(content = "菜单", operateType = OperateType.UPDATE)
	public AjaxRes updateMenuStates(@RequestParam("menuIds") List<Integer> menuIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.menuService.updateSlfMenuStates(menuIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询菜单
	 * 
	 * @param map
	 *            查询条件
	 * @return 菜单集合
	 */
	@RequestMapping("/getMenuList")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes getMenuList(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfMenu> slfMenus = this.menuService.getSlfMenuList(map);
			ajaxRes.setSucceed(slfMenus, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 分页查询菜单
	 * 
	 * @param map
	 *            条件集合
	 * @return 分页对象
	 */
	@RequestMapping("/slicedQueryMenu")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes slicedQueryMenu( @RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfMenu> slfMenus = this.menuService.getSlfMenuList(map);
			ajaxRes.setSucceed(slfMenus, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取下一排序号
	 * 
	 * @param parentId
	 *            父ID
	 * @return 排序号
	 */
	@RequestMapping("/getNextSequence")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes getNextSequence(@RequestParam(value = "parentId", required = false) Integer parentId, AjaxRes ajaxRes) {
		try {
			Long sequence = this.menuService.getNextSequence(parentId);
			ajaxRes.setSucceed(sequence, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询菜单树
	 * 
	 * @return 菜单树
	 */
	@RequestMapping("/getMenuTreeList")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes getMenuTreeList(AjaxRes ajaxRes) {
		try {
			List<TreeDto> treeDtos = this.menuService.getMenuTreeList();
			ajaxRes.setSucceed(treeDtos, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询菜单树
	 * 
	 * @return 菜单树
	 */
	@RequestMapping("/getMenuSelectTreeList")
	@LogInfo(content = "菜单", operateType = OperateType.QUERY)
	public AjaxRes getMenuSelectTreeList(@RequestParam("roleId") Integer roleId, AjaxRes ajaxRes) {
		try {
			Map<String, Object> map = this.menuService.getMenuSelectTreeList(roleId);
			ajaxRes.setSucceed(map, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询员工所有的功能菜单
	 * 
	 * @return 菜单
	 */
	@RequestMapping("/getEmployeeMenuList")
	public AjaxRes getEmployeeMenuList(AjaxRes ajaxRes) {
		try {
			List<MenuDto> slfMenus = this.menuService.getEmployeeMenuList(ThreadLocalUtil.getEmployee().getEmployeeId());
			ajaxRes.setSucceed(slfMenus, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询员功能菜单按钮
	 * 
	 * @param parentId
	 *            菜单ID
	 * @return 按钮
	 */
	@RequestMapping("getEmployeeMenuButtonList")
	@LogInfo(content = "按钮", operateType = OperateType.QUERY)
	public AjaxRes getEmployeeMenuButtonList(@RequestParam("parentId") Integer parentId, AjaxRes ajaxRes) {
		try {
			Map<String, Boolean> slfMenus = this.menuService.getEmployeeMenuButtonList(ThreadLocalUtil.getEmployee().getEmployeeId(), parentId);
			ajaxRes.setSucceed(slfMenus, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
